package cn.wanda.bi.sunshine.platform.server.service.authorize;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import cn.wanda.bi.sunshine.platform.server.client.request.MsgBody;
import cn.wanda.bi.sunshine.platform.server.client.request.MsgContent;
import cn.wanda.bi.sunshine.platform.server.client.request.OpenimSendMsgRequest;
import cn.wanda.bi.sunshine.platform.server.client.wrapper.FFanLiveClientWrapper;
import cn.wanda.bi.sunshine.platform.server.client.wrapper.TencentIMClientWrapper;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.SimpleResponse;
import io.swagger.annotations.ApiParam;

/**
 * Created by wangfupeng1
 */
@Service
public class TencentOpenimService {

    public static final Logger logger = LoggerFactory.getLogger(TencentOpenimService.class);

    @Autowired
    private TencentIMClientWrapper tencentIMClientWrapper;

    @Autowired
    private FFanLiveClientWrapper fFanLiveClientWrapper;
    
    @Value("${client.tencent.im.sdkappid}")
    private Integer sdkappid;
    @Value("${client.tencent.im.admin_sig}")
    private String adminSig;
    @Value("${client.tencent.im.admin_identifier}")
    private String adminIdentifier;

	public String openimSendmsg(OpenimSendMsgRequest request, String identifier,
			int sdkAppId, String contentType, int random, String usersig){
        return tencentIMClientWrapper.openimSendmsg(request, identifier, sdkAppId, contentType, random, usersig);
    }

    public SimpleResponse sendmsg(
    		@ApiParam(value = "uid") @RequestParam("uid") String uid,
    		@ApiParam(value = "loginToken") @RequestParam("loginToken") String loginToken,
    		@ApiParam(value = "appid") @RequestParam("appid") String appid,
    		@ApiParam(value = "data") @RequestParam("data") String data
            ) {
//    	AuthRequest authRequest = new AuthRequest();
//    	authRequest.setAppid(appid);
//    	authRequest.setLoginToken(loginToken);
//    	authRequest.setUid(uid);
//        SimpleResponse simpleResponse = fFanLiveClientWrapper.liveV1Signature(authRequest);
//        Map<String,String> usersigMap = (Map<String,String>) simpleResponse.getData();
//        if(usersigMap==null)
//        	throw new SunshineException(SunshineCode.UID_LOGINTOKEN_APPID_ERROR_CANNOT_GET_SIGNATURE);
        OpenimSendMsgRequest openimSendMsgRequest = new OpenimSendMsgRequest();
        Random random = new Random();
        openimSendMsgRequest.setMsgRandom(random.nextInt(100000000));
        Long time1=Math.floorDiv(Calendar.getInstance().getTime().getTime(),1000);
        System.out.println("\n============\n"+time1+"\n============\n");
        openimSendMsgRequest.setMsgTimeStamp(time1);
        openimSendMsgRequest.setSyncOtherMachine(2);
        openimSendMsgRequest.setTo_Account(uid);
        MsgContent msgContent = new MsgContent(data);
        MsgBody msgBody = new MsgBody();
        msgBody.setMsgContent(msgContent);
        msgBody.setMsgType("TIMCustomElem");
        List<MsgBody> msgBodys = new ArrayList<MsgBody>();
        msgBodys.add(msgBody);
        openimSendMsgRequest.setMsgBody(msgBodys);
        return SimpleResponse.success(openimSendmsg(openimSendMsgRequest,adminIdentifier,sdkappid,"json",random.nextInt(100000000),adminSig));
    }
    
}
